package com.omazan.facace;

import com.omazan.db.HandleAdmins;
import com.omazan.db.HandleCustomers;
import com.omazan.db.HandleEmployees;
import com.omazan.db.HandleUsers;
import com.omazan.db.models.Administrator;
import com.omazan.db.models.Customer;
import com.omazan.db.models.Employee;
import com.omazan.db.models.User;
import com.omazan.facace.models.RegisterDetails;
import com.omazan.utils.OmazanException;

public class CustomerActions {

	public boolean registerCustomer(RegisterDetails registerDetails)
			throws OmazanException {

		User user = new User();
		Customer customer = new Customer();
		Administrator administrator = new Administrator();
		Employee employee = new Employee();

		user.setActive(true);
		// TODO: Prasanna : confirm same password is entered.

		user.setPassword(registerDetails.userPassword);
		user.setUserName(registerDetails.userName);

		if (null == registerDetails.userType) {
			user.getUserType().setUserTypeId(3);
		} else {
			if (registerDetails.userType
					.equals(com.omazan.utils.Constants.admin)) {
				user.getUserType().setUserTypeId(1);
				administrator.setActive(true);
				administrator.setCity(registerDetails.city);
				administrator.setCountry(registerDetails.country);

				administrator.setEmail(registerDetails.email);
				administrator.setFirstName(registerDetails.firstName);
				administrator.setHouseNo(registerDetails.houseNo);
				administrator.setLastName(registerDetails.lastName);
				administrator.setMobileNo(registerDetails.mobileNo);
				administrator.setState(registerDetails.state);
				administrator.setStreet(registerDetails.street);
				administrator.setUser(user);

				new HandleUsers().insertUser(user);
				new HandleAdmins().insertAdmin(administrator);
			} else if (registerDetails.userType
					.equals(com.omazan.utils.Constants.emp)) {
				user.getUserType().setUserTypeId(2);
				employee.setActive(true);
				employee.setCity(registerDetails.city);
				employee.setCountry(registerDetails.country);

				employee.setEmail(registerDetails.email);
				employee.setFirstName(registerDetails.firstName);
				employee.setHouseNo(registerDetails.houseNo);
				employee.setLastName(registerDetails.lastName);
				employee.setMobileNo(registerDetails.mobileNo);
				employee.setState(registerDetails.state);
				employee.setStreet(registerDetails.street);
				employee.setUser(user);

				new HandleUsers().insertUser(user);
				new HandleEmployees().insertEmployee(employee);
			} else {
				user.getUserType().setUserTypeId(3);
				customer.setActive(true);
				customer.setCity(registerDetails.city);
				customer.setCountry(registerDetails.country);

				customer.setEmail(registerDetails.email);
				customer.setFirstName(registerDetails.firstName);
				customer.setHouseNo(registerDetails.houseNo);
				customer.setLastName(registerDetails.lastName);
				customer.setMobileNo(registerDetails.mobileNo);
				customer.setState(registerDetails.state);
				customer.setStreet(registerDetails.street);
				customer.setUser(user);

				new HandleUsers().insertUser(user);
				new HandleCustomers().insertCustomer(customer);
			}
		}

		return true;

	}
}
